<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 数据类型Python 中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。在 Python 中，变量就是变量，它没有类型，我们所说的”类型”是变量所指的内存中对象的类型。等号（=）用来给变量赋值。等号（=）运算符左边是一个变量名,等号（=）运算符右边是存储在变量中的值。多变量赋值：a = b = c = 1 或者 a, b, c = 1, 2, &amp;quot;">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Python-学习(二)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Python/Python-学习(二)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 数据类型Python 中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。在 Python 中，变量就是变量，它没有类型，我们所说的”类型”是变量所指的内存中对象的类型。等号（=）用来给变量赋值。等号（=）运算符左边是一个变量名,等号（=）运算符右边是存储在变量中的值。多变量赋值：a = b = c = 1 或者 a, b, c = 1, 2, &amp;quot;">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-0589cc65f4d5c930.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-459f6a13102c01cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-e51979eeb77f2447.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-c118d8e22503bbf1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-0b44b3cd957edbfb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-0afa44f45b683723.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-3e71fe93684e1037.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-125f73c75b44bee8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-fd2738f8a0cca6f5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-9747407408b35010.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-27f0a38279ef2066.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T10:31:52.680Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Python-学习(二)">
<meta name="twitter:description" content="####1. 数据类型Python 中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。在 Python 中，变量就是变量，它没有类型，我们所说的”类型”是变量所指的内存中对象的类型。等号（=）用来给变量赋值。等号（=）运算符左边是一个变量名,等号（=）运算符右边是存储在变量中的值。多变量赋值：a = b = c = 1 或者 a, b, c = 1, 2, &amp;quot;">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-0589cc65f4d5c930.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Python/Python-学习(二)/"/>





  <title>Python-学习(二) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Python/Python-学习(二)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Python-学习(二)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:39+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 数据类型<br>Python 中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。在 Python 中，变量就是变量，它没有类型，我们所说的”类型”是变量所指的内存中对象的类型。等号（=）用来给变量赋值。等号（=）运算符左边是一个变量名,等号（=）运算符右边是存储在变量中的值。<br><strong>多变量赋值：<code>a = b = c = 1</code> 或者 <code>a, b, c = 1, 2, &quot;mazaiting&quot;</code></strong><br>Python中六个标准数据类型：Number（数字）、String（字符串）、List（列表）、Tuple（元组）、Sets（集合）、Dictionary（字典）</p>
<ul>
<li>Number（数字）<br>1). 数字类型有四种 int、float、bool、complex（复数）。<br>示例1：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; a, b, c, d = 210, 4.4, True, 4+3j</span><br><span class="line">&gt;&gt;&gt; print(type(a),type(b),type(c),type(d))</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>打印结果1：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-0589cc65f4d5c930.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"><br><a id="more"></a></p>
<p>示例2：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; a = 11</span><br><span class="line">&gt;&gt;&gt; isinstance(a,int)</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-459f6a13102c01cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"><br><strong>区别:</strong><br>type()不会认为子类是一种父类类型。<br>isinstance()会认为子类是一种父类类型。</p>
<p>2). 删除对象引用<code>del var</code></p>
<p><strong>注意:</strong><br>1、Python可以同时为多个变量赋值，如a, b = 1, 2。<br>2、一个变量可以通过赋值指向不同类型的对象。<br>3、数值的除法（/）总是返回一个浮点数，要获取整数使用//操作符。<br>4、在混合计算时，Python会把整型转换成为浮点数。</p>
<ul>
<li>String（字符串）<br>Python中的字符串用单引号<code>&#39;</code>或双引号<code>&quot;</code>括起来，同时使用反斜杠<code>\</code>转义特殊字符。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">str = &apos;Mazaiting&apos;</span><br><span class="line"> </span><br><span class="line">print (str)          # 输出字符串</span><br><span class="line">print (str[0:-1])    # 输出第一个到倒数第二个的所有字符</span><br><span class="line">print (str[0])       # 输出字符串第一个字符</span><br><span class="line">print (str[2:5])     # 输出从第三个开始到第五个的字符</span><br><span class="line">print (str[2:])      # 输出从第三个开始的后的所有字符</span><br><span class="line">print (str * 2)      # 输出字符串两次</span><br><span class="line">print (str + &quot;TEST&quot;) # 连接字符串</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p><strong>注意</strong><br>1、反斜杠可以用来转义，使用r可以让反斜杠不发生转义。<br>2、字符串可以用+运算符连接在一起，用*运算符重复。<br>3、Python中的字符串有两种索引方式，从左往右以0开始，从右往左以-1开始。<br>4、Python中的字符串不能改变。</p>
<ul>
<li>List（列表）<br>列表可以完成大多数集合类的数据结构实现。列表中元素的类型可以不相同，它支持数字，字符串甚至可以包含列表（所谓嵌套）。列表是写在方括号([])之间、用逗号分隔开的元素列表。和字符串一样，列表同样可以被索引和截取，列表被截取后返回一个包含所需元素的新列表。<br>示例1：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"># 列表数据</span><br><span class="line">list = [&apos;abcd&apos;, 784, 2.234, &apos;mazaiting&apos;, 20.2]</span><br><span class="line"># 短列表</span><br><span class="line">tinylist = [234, &apos;zaitingma&apos;]</span><br><span class="line"></span><br><span class="line"># 输出完整列表</span><br><span class="line">print(list)</span><br><span class="line"># 输出列表第一个元素</span><br><span class="line">print(list[0])</span><br><span class="line"># 从第二个开始输出到第三个元素</span><br><span class="line">print(list[1:3])</span><br><span class="line"># 输出从第三个元素开始的所有元素</span><br><span class="line">print(list[2:])</span><br><span class="line"># 连续输出两次列表</span><br><span class="line">print(tinylist * 2)</span><br><span class="line"># 连接列表</span><br><span class="line">print(list + tinylist)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>打印结果1：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-e51979eeb77f2447.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"></p>
<p>示例2：<br>修改列表数据：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; a = [1, 2, 3, 4, 5]     # 定义列表</span><br><span class="line">&gt;&gt;&gt; a[0] = 9                   # 为第一个元素赋值</span><br><span class="line">&gt;&gt;&gt; a                                # 打印列表</span><br><span class="line">[9, 2, 3, 4, 5]</span><br><span class="line">&gt;&gt;&gt; a[2:5] = [14,13,14]  # 修改第3-5个列表数据</span><br><span class="line">&gt;&gt;&gt; a                                # 打印</span><br><span class="line">[9, 2, 14, 13, 14]</span><br><span class="line">&gt;&gt;&gt; a[2:5] = []               # 将第3-5个数据置为空</span><br><span class="line">&gt;&gt;&gt; a                               # 打印</span><br><span class="line">[9, 2]</span><br></pre></td></tr></table></figure></p>
<ul>
<li>Tuple（元组）<br>元组（tuple）与列表类似，不同之处在于元组的元素不能修改。元组写在小括号(())里，元素之间用逗号隔开。<br>示例1：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># 定义元组</span><br><span class="line">tuple = (&apos;abcd&apos;, 432, 3.32, &apos;mazaiting&apos;, 240.2)</span><br><span class="line">tinytuple = (123, &apos;zaitingma&apos;)</span><br><span class="line"></span><br><span class="line"># 输出完整元组</span><br><span class="line">print(tuple)</span><br><span class="line"># 输出元组的第一个元素</span><br><span class="line">print(tuple[0])</span><br><span class="line"># 输出从第二个元素到第三个元素</span><br><span class="line">print(tuple[1:3])</span><br><span class="line"># 输出从第三个元素开始的所有元素</span><br><span class="line">print(tuple[2:])</span><br><span class="line"># 输出两次元组</span><br><span class="line">print(tinytuple)</span><br><span class="line"># 连接元组</span><br><span class="line">print(tuple+tinytuple)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>打印结果1：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-c118d8e22503bbf1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"></p>
<p>示例2：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; tup = (1, 2, 3, 4, 5, 6)</span><br><span class="line">&gt;&gt;&gt; print(tup[0])</span><br><span class="line">1</span><br><span class="line">&gt;&gt;&gt; print(tup[1:5])</span><br><span class="line">(2, 3, 4, 5)</span><br><span class="line">&gt;&gt;&gt; tup[0] = 11         # 修改元组数据是非法的</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;</span><br><span class="line">TypeError: &apos;tuple&apos; object does not support item assignment</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p><strong>string、list和tuple都属于sequence（序列）。</strong></p>
<p><strong>注意：</strong><br>1、与字符串一样，元组的元素不能修改。<br>2、元组也可以被索引和切片，方法一样。<br>3、注意构造包含0或1个元素的元组的特殊语法规则。<br>4、元组也可以使用+操作符进行拼接。</p>
<ul>
<li>Sets（集合）<br>1). 集合（set）是一个无序不重复元素的序列。<br>2). 基本功能是进行成员关系测试和删除重复元素。<br>3). 可以使用大括号 { } 或者 set() 函数创建集合，注意：创建一个空集合必须用 set() 而不是 { }，因为 { } 是用来创建一个空字典。<br>示例：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"># 定义集合</span><br><span class="line">student = &#123;&apos;Tom&apos;, &apos;Jim&apos;, &apos;Mary&apos;, &apos;Tom&apos;, &apos;Jack&apos;, &apos;Rose&apos;&#125;</span><br><span class="line"># 输出集合，重复的元素被自动去掉</span><br><span class="line">print(student) </span><br><span class="line"># 成员测试</span><br><span class="line">if(&apos;Rose&apos; in student) :</span><br><span class="line">	print(&apos;Rose 在集合中&apos;)</span><br><span class="line">else :</span><br><span class="line">	print(&apos;Rose 不在集合中&apos;)</span><br><span class="line"># set 可以进行集合运算</span><br><span class="line">a = set(&apos;abracdabra&apos;)</span><br><span class="line">b = set(&apos;alacazam&apos;)</span><br><span class="line"></span><br><span class="line">print(a)</span><br><span class="line"></span><br><span class="line"># a和b的差集</span><br><span class="line">print(a - b)</span><br><span class="line"># a和b的并集</span><br><span class="line">print(a | b)</span><br><span class="line"># a和b的交集</span><br><span class="line">print(a &amp; b)</span><br><span class="line"># a和b不同时存在的元素</span><br><span class="line">print(a ^ b)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-0b44b3cd957edbfb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图5.png"></p>
<ul>
<li>Dictionary（字典）<br>字典（dictionary）是Python中另一个非常有用的内置数据类型。列表是有序的对象集合，字典是无序的对象集合。两者之间的区别在于：字典当中的元素是通过键来存取的，而不是通过偏移存取。字典是一种映射类型，字典用”{ }”标识，它是一个无序的键(key) : 值(value)对集合。键(key)必须使用不可变类型。在同一个字典中，键(key)必须是唯一的。<br>示例：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"># 创建字典</span><br><span class="line">dict = &#123;&#125;</span><br><span class="line">dict[&apos;one&apos;] = &quot;123&quot;</span><br><span class="line">dict[2] = &quot;23434&quot;</span><br><span class="line"></span><br><span class="line">tinydict = &#123;&apos;name&apos;:&apos;mazaiting&apos;,&apos;code&apos;:1,&apos;site&apos;:&apos;xibeixing&apos;&#125;</span><br><span class="line"></span><br><span class="line"># 输出键为&apos;one&apos;的值</span><br><span class="line">print(dict[&apos;one&apos;])</span><br><span class="line"># 输出键为2的值</span><br><span class="line">print(dict[2])</span><br><span class="line"># 输出完整的字典</span><br><span class="line">print(tinydict)</span><br><span class="line"># 输出所有键</span><br><span class="line">print(tinydict.keys())</span><br><span class="line"># 输出所有值</span><br><span class="line">print(tinydict.values())</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-0afa44f45b683723.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图6.png"><br><strong>注意：</strong><br>1、字典是一种映射类型，它的元素是键值对。<br>2、字典的关键字必须为不可变类型，且不能重复。<br>3、创建空字典使用 { }。</p>
<p>####2. 运算符<br>1). 逻辑运算符<br>and（和）、or（或）、not（非）<br>2). 成员运算符–用于判断某个元素是否在某个序列中存在</p>
<ul>
<li>in：如果在指定的序列中找到值返回 True，否则返回 False。</li>
<li>not in：如果在指定的序列中没有找到值返回 True，否则返回 False。<br>3). 身份运算符–用于判断两个对象是否引用自同一对象</li>
<li>is：is 是判断两个标识符是不是引用自一个对象 x is y</li>
<li>is not：is not 是判断两个标识符是不是引用自不同对象</li>
</ul>
<p>####3. 字符串<br>1). 字符串格式化<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 字符串格式化</span><br><span class="line">print(&quot;我叫 %s 今年 %d 岁！&quot; % (&apos;mazaiting&apos;, 24))</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-3e71fe93684e1037.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图7.png"><br>2). 三引号<br>python三引号允许一个字符串跨多行，字符串中可以包含换行符、制表符以及其他特殊字符。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">para_str = &quot;&quot;&quot; 这个一个多行字符串</span><br><span class="line">多行字符串可以使用制表符</span><br><span class="line">\t制表符</span><br><span class="line">\n也可以使用换行符</span><br><span class="line">&quot;&quot;&quot;</span><br><span class="line">print(para_str)</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-125f73c75b44bee8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图8.png"></p>
<p>####4. 列表<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"># 创建列表</span><br><span class="line">list1 = [&apos;Google&apos;, &apos;mazaiting&apos;, 1204, 10.3]</span><br><span class="line"># 获取值</span><br><span class="line">print(&apos;list1[0]:&apos;, list1[0])</span><br><span class="line"># 追加元素</span><br><span class="line">list1.append(&apos;1425941077&apos;)</span><br><span class="line"># 打印全部数据</span><br><span class="line">print(list1)</span><br><span class="line"># 删除第一条数据</span><br><span class="line">del list1[1]</span><br><span class="line"># 打印全部数据</span><br><span class="line">print(list1)</span><br><span class="line"># 迭代</span><br><span class="line">for x in [1,2,3]:</span><br><span class="line">	print(x, end=&quot; &quot;)</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-fd2738f8a0cca6f5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图9.png"></p>
<p>####5. 元组<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"># 访问元组</span><br><span class="line">tup1 = (&apos;Google&apos;, &apos;Runoob&apos;, 1992, 2000)</span><br><span class="line">print (&quot;tup1[0]: &quot;, tup1[0])</span><br><span class="line"></span><br><span class="line"># 修改元组.元素是不允许被修改的，但可以对元组进行拼接</span><br><span class="line">tup2 = (&apos;abc&apos;, &apos;xyz&apos;)</span><br><span class="line">tup3 = tup1 + tup2</span><br><span class="line">print (tup3)</span><br><span class="line"></span><br><span class="line"># 删除元组.元组中的元素是不允许被删除的，但我们可以删除整个元组</span><br><span class="line">del tup3</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-9747407408b35010.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图10.png"></p>
<p>####6. 字典<br>字典是另一种可变容器模型，且可存储任意类型对象。<br>字典的每个键值(key=&gt;value)对用冒号(:)分割，每个对之间用逗号(,)分割，整个字典包括在花括号({})中 ,格式如下所示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;key1 : value1, key2 : value2 &#125;</span><br></pre></td></tr></table></figure></p>
<p>键必须是唯一的，但值则不必。</p>
<p>示例：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># 访问字典里的值</span><br><span class="line">dict = &#123;&apos;Name&apos;:&quot;mazaiting&quot;, &apos;Age&apos;:24, &apos;Type&apos;:&apos;Android&apos;&#125;</span><br><span class="line">print(&quot;dict[&apos;Name&apos;]:&quot;, dict[&apos;Name&apos;])</span><br><span class="line"></span><br><span class="line"># 修改字典内容</span><br><span class="line">dict[&apos;Type&apos;] = &apos;JavaEE&apos;</span><br><span class="line">print(&quot;dict[&apos;Type&apos;]&quot;, dict[&apos;Type&apos;])</span><br><span class="line"></span><br><span class="line"># 删除字典元素</span><br><span class="line">del dict[&apos;Name&apos;] # 删除键&apos;Name&apos;</span><br><span class="line">print(dict)</span><br><span class="line">dict.clear() 	 # 清空字典</span><br><span class="line">print(dict)</span><br><span class="line">del dict 		 # 删除字典</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-27f0a38279ef2066.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图11.png"></p>
<p><strong>字典键的特性:</strong><br>1). 不允许同一个键出现两次。创建时如果同一个键被赋值两次，后一个值会被记住.<br>2). 键必须不可变，所以可以用数字，字符串或元组充当，而用列表就不行.</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Python/Python-学习(九)--多线程/" rel="next" title="Python-学习(九)--多线程">
                <i class="fa fa-chevron-left"></i> Python-学习(九)--多线程
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Python/Python-学习(五)/" rel="prev" title="Python-学习(五)">
                Python-学习(五) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
